var Page = function(){
    this.init();
};
Page.prototype = {
    params: {
        passport: '',
        page: 1,
        listRows: 10,
        total: 0,
        mobile: '',
        memberNumber: '',
        memberName:'',
        realName:'',
        isEdit:null,
        memberId:null
    },
    init: function(){
        this.initParams();
        this.initEvent();
    },
    initParams: function(){
        this.params.passport = Pub.getCookie('_PASSPORT');
    },
     getData: function() {
        var _this = this;
        var paramsObj = {
            passport: _this.params.passport,
            page: _this.params.page,
            listRows: _this.params.listRows,
            mobile: _this.params.mobile,
            order: _this.params.order,
            memberNumber: _this.params.memberNumber,
            memberName: _this.params.memberName,

        };
        console.log(paramsObj);
        Api.memberinfolist(paramsObj, function(data) {
            console.log(data);
            _this.getDataList(data.list);
            _this.params.total = data.totalPages;
            layui.use('laypage', function() {
                var laypage = layui.laypage;
                //执行一个laypage实例
                laypage.render({
                    elem: 'page',
                    index: 1,
                    theme: '#3498db',
                    curr: _this.params.page,
                    count: _this.params.total, //数据总数，从服务端得到
                    hash: 'curr_page',
                    layout: ['prev', 'page', 'next', 'skip'],
                    jump: function(obj, first) {
                        _this.params.page = obj.curr;
                        //obj包含了当前分页的所有参数，比如：curr当前页
                        var Params = {
                            passport: _this.params.passport,
                            page: _this.params.page,
                            listRows: _this.params.listRows,
                            mobile: _this.params.mobile,
                            order: _this.params.order,
                            memberNumber: _this.params.memberNumber,
                            memberName: _this.params.memberName,
                        };
                        Api.memberinfolist(Params, function(data) {
                            _this.getDataList(data.list);

                        })
                    }
                });
            });
        });
    },
    getDataList: function(data) {
        var _this = this;
        if (data && data.length > 0) {
            var html = '',
                i = 0,
                len = data.length;
            for (i; i < len; i++) {
                html += '<tr>' +
                        '<td class="text-c">' + data[i].memberNumber + '</td>' +
                        '<td class="text-c">' + data[i].memberName + '</td>'+
                        '<td class="text-c">' + data[i].mobile + '</td>';


                html += '<td class="text-c">' +
                    '<p><a memberId="' + data[i].memberId + '" class="handle view">查看</a><a memberId="' + data[i].memberId + '" class="handle edit">编辑</a>';
                if (data[i].isUse == 0) {
                    html+='<a memberId="' + data[i].memberId + '"  isUse="1" class="c-666 isUse" style="padding:2px 5px;">恢复</a>';
                }else{
                    html+='<a memberId="' + data[i].memberId + '"  isUse="0" class="handle isUse">冻结</a>';
                };
                    html+='</p>' +
                    '</td>' +
                    '</tr>';
            }
            $('#List').html(html);
            $('tfoot').css('display', 'table-footer-group');
            _this.getDataEvent();
        } else {
            var html = '<tr>' +
                '<td class="text-c c-666" colspan="4" >暂无数据</td>' +
                '</tr>';
            $('#List').html(html);
            $('tfoot').css('display', 'none');
        }
    },
    getDataEvent: function() {
        var _this = this;
        honeySwitch.init();
        $('.view').on('click', function() {
            Pub.setSession('memberId', $(this).attr('memberId'));
            window.location.href = Pub.getHtmlUrl('./person-list-detail.html');
        });
        $('.edit').on('click', function() {
            $('#modal_search').modal('show');
            var memberId=$(this).attr('memberId');
            _this.params.memberId=memberId;
            _this.params.isEdit=1;
            _this.getMemberInfo(memberId);
        });
        $('.isUse').on('click', function() {
            var memberId=$(this).attr('memberId');
            var isUse=$(this).attr('isUse');
            var Params = {
                passport: _this.params.passport,
                memberId:memberId
            };
            console.log(Params);
            if (isUse==1) {
                layui.use('layer', function(){
                    layer.confirm('是否恢复？', {
                        btn: ['确定','取消'] //按钮
                    }, function(index){
                        _this.isfreeze(memberId,isUse);
                        layer.close(index);
                    }, function(){

                    });
                });
            }else{
                layui.use('layer', function(){
                    layer.confirm('是否冻结？', {
                        btn: ['确定','取消'] //按钮
                    }, function(index){
                        _this.isfreeze(memberId,isUse);
                        layer.close(index);
                    }, function(){

                    });
                });
            };

        });
    },
    isfreeze:function(memberId,isUse){
        var _this = this;
        var Params = {
            passport: _this.params.passport,
            memberId:memberId,
            isUse:parseInt(isUse)
        };
        console.log(Params);
        Api.isfreeze(Params, function(data) {
            _this.getData();
        })
    },
    getMemberInfo:function(memberId){
        var _this = this;
        var Params = {
            passport: _this.params.passport,
            memberId:memberId,
        };
        console.log(Params);
        Api.memberlistmodifydetails(Params, function(data) {
            $('#mo_mobile').val(data.mobile);
            $('#mo_memberAddress').val(data.memberAddress);
            $('#mo_memberName').val(data.memberName);
        })
    },
    addOrEditMember:function(){
        var _this = this;
        var mobile=$('#mo_mobile').val();
        var memberAddress=$('#mo_memberAddress').val();
        var memberName=$('#mo_memberName').val();
        if (_this.params.isEdit==1) {
            var Params = {
                passport: _this.params.passport,
                memberId:parseInt(_this.params.memberId),
                mobile:mobile,
                memberAddress:memberAddress,
                memberName:memberName
            };
            console.log(Params);
            Api.memberlistmodify(Params, function(data) {
                _this.getData();
                layui.use('layer', function(){
                    layer.msg('编辑成功', {icon: 6});
                });
                $('#modal_search').modal('hide');
            })
        }else{
            var Params = {
                passport: _this.params.passport,
                mobile:mobile,
                memberAddress:memberAddress,
                memberName:memberName
            };
            Api.memberlistadd(Params, function(data) {
                _this.getData();
                layui.use('layer', function(){
                    layer.msg('添加成功', {icon: 6});
                });
                $('#modal_search').modal('hide');
            })
        };
    },
    otherEvent:function(){
        var _this = this;
        $('.goods-checkbox input').iCheck({
            checkboxClass: 'icheckbox-blue',
            radioClass: 'iradio-blue',
            increaseArea: '20%'
        })
        $('.slectShow').click(function(event) {
            $('.select-wrap').toggle(400);
        });
        $('#changeSize').on('change', function(event) {
            event.preventDefault();
            _this.params.listRows = $(this).val();
            _this.params.page=1;
            $('#page').html('');
            _this.getData();
        });
        $('#changeDesc').on('change', function(event) {
            event.preventDefault();
            if ($(this).val() !== '排序方式') {
                _this.params.order = $(this).val();
                $('#page').html('');
                _this.params.page=1;
                _this.getData();
            };
        });
        $('#normalSearch').click(function(event) {
            _this.params.mobile = $('#mobile').val();
            _this.params.memberNumber = $('#memberNumber').val();
            _this.params.memberName = $('#memberName').val();
            _this.params.page = 1;
            _this.getData();
        });
        $('#addMember').click(function(event) {
            _this.params.isEdit=0;
            $('#modal_search').modal('show');
            $('#mo_mobile').val('');
            $('#mo_memberAddress').val('');
            $('#mo_memberName').val('');
        });
        $('#addOrEdit').click(function(event) {
            _this.addOrEditMember();
        });
    },
    initEvent: function(){
        var _this = this;
        _this.getData();
        _this.otherEvent();


    }
};
var page = new Page();